Create Excel Histogram Chart in C# | Step‑by‑Step Tutorial

2026-12-03 03:34:04 Written by  Jane Zhao
Rate this item
(0 votes)

Step‑by‑Step .NET tutorial to create a histogram in Excel

When analyzing numeric datasets such as exam scores, sales figures or sensor readings, professionals often need to visualize data distribution. A histogram chart is the standard statistical chart for this purpose.

With Spire.XLS for .NET, C# developers can programmatically create a histogram chart in Excel without installing Microsoft Office on the runtime machine. This tutorial covers:

What Is a Histogram Chart?

A histogram chart is a statistical chart that shows how numerical data is distributed across a series of ranges called bins. Instead of plotting every individual value, a histogram groups values into intervals and uses each bar's height to show how many values fall into that interval.

In C# automation scenarios, histogram charts help developers quickly reveal a dataset’s central tendency, value spread, shape, and outliers for reporting purposes.

A typical histogram has three core elements:

Element What It Represents
X‑axis The numerical bin intervals — for example, [160, 168), [168, 176), [176, 184)
Y‑axis The frequency or count of values that fall into each bin
Bars One bar per bin; the bar height equals the number of observations in that interval

Unlike a standard column chart, histogram bars usually touch or have a very small gap because the x‑axis represents a continuous numerical range, not separate categories.

Setting Up Your Project

Before you start, make sure you have:

  • Visual Studio (or any .NET IDE)
  • .NET Framework 4.5+, .NET Core, or .NET 5+
  • Spire.XLS 11.10.5 or later installed via NuGet

Install the package from the Package Manager Console:

Install-Package Spire.XLS

Step-by-Step: C# Histogram Chart Generation

Step 1: Load or Create a Workbook with Data

A histogram requires a range of numerical values. If you already have an Excel file with source data, load it as follows:

Workbook workbook = new Workbook();
workbook.LoadFromFile("Data.xlsx");
Worksheet sheet = workbook.Worksheets[0];

If you prefer to build the data in code, you can create a new workbook and write an array of numbers into a column. Example:

Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];

// Sample raw numeric data: student exam scores
sheet.Range["A1"].Text = "Exam Scores";
double[] scores = { 55, 62, 68, 70, 72, 75, 77, 80, 82, 85, 88, 90, 91, 94, 96 };
for (int i = 0; i < scores.Length; i++)
{
    sheet.Range[$"A{i + 2}"].NumberValue = scores[i];
}

Step 2: Add a Chart Object and Set the Type to Histogram

With the data in place, add a chart to the worksheet and explicitly set its type to ExcelChartType.Histogram. This is the core step that tells Spire.XLS to render a histogram rather than a standard column chart.

Chart histogramChart = sheet.Charts.Add();
histogramChart.ChartType = ExcelChartType.Histogram;

✅ Remarks: The ExcelChartType enumeration contains over 70 chart types, including pie chart, bar chart, waterfall chart, etc.

Step 3: Bind the Data Range to Chart

Next, assign the numerical data range to the chart. For a histogram, you typically select a single column of values — the chart engine automatically calculates the frequency distribution for you.

histogramChart.DataRange = sheet.Range["A1:A16"];

Step 4: Configure the Bins (Axis Grouping)

Spire.XLS gives you two primary ways to control binning on the category axis. Enable only one of the following:

// Option A: Set a fixed bin width
histogramChart.PrimaryCategoryAxis.BinWidth = 10;
// Option B: Specify the number of bins
histogramChart.PrimaryCategoryAxis.NumberOfBins = 5;

Step 5: Format the Chart for Readability

Default generated charts often need adjustment for presentation: bar gap, titles, legend, position, and data labels.

// Set Gap Width
histogramChart.Series[0].DataFormat.Options.GapWidth = 6;

// Add Chart and Axis Titles
histogramChart.ChartTitle = "Distribution of Exam Scores";
histogramChart.PrimaryValueAxis.Title = "Frequency";
histogramChart.PrimaryCategoryAxis.Title = "Score Bins";

// Chart position
histogramChart.LeftColumn = 3;
histogramChart.TopRow = 3;
histogramChart.RightColumn = 10;
histogramChart.BottomRow = 20;

// Hide the Legend
histogramChart.HasLegend = false;

// Show data labels
var labels = histogramChart.Series[0].DataPoints.DefaultDataPoint.DataLabels;
labels.HasValue = true;
labels.Size = 9;

Step 6: Save the Workbook

Finally, save the workbook to an .xlsx file. Spire.XLS supports multiple Excel versions, but as histogram charts require Excel 2016 or later; save as ExcelVersion.Version2016 or newer.

workbook.SaveToFile("Histogram_chart.xlsx", ExcelVersion.Version2016);

Full C# Code for Creating a Histogram in Excel

using Spire.Xls;

namespace HistogramChart
{
    class Program
    {
        static void Main(string[] args)
        {

            // Initialize workbook and worksheet
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];

            // Sample raw numeric data: student exam scores
            sheet.Range["A1"].Text = "Exam Scores";
            double[] scores = { 55, 62, 68, 70, 72, 75, 77, 80, 82, 85, 88, 90, 91, 94, 96 };
            for (int i = 0; i < scores.Length; i++)
            {
                sheet.Range[$"A{i + 2}"].NumberValue = scores[i];
            }

            // Add histogram chart to sheet
            Chart histogramChart = sheet.Charts.Add();
            histogramChart.ChartType = ExcelChartType.Histogram;

            // Bind data range to the histogram
            histogramChart.DataRange = sheet.Range["A1:A16"];

            // Set a Fixed Bin Width
            histogramChart.PrimaryCategoryAxis.BinWidth = 10;
            // (Optional) Specify the Number of Bins
            // histogramChart.PrimaryCategoryAxis.NumberOfBins = 5;

            // Set Gap Width
            histogramChart.Series[0].DataFormat.Options.GapWidth = 6;

            // Add Chart and Axis Titles
            histogramChart.ChartTitle = "Distribution of Exam Scores";
            histogramChart.PrimaryValueAxis.Title = "Frequency";
            histogramChart.PrimaryCategoryAxis.Title = "Score Bins";

            // Chart position
            histogramChart.LeftColumn = 3;
            histogramChart.TopRow = 3;
            histogramChart.RightColumn = 10;
            histogramChart.BottomRow = 20;

            // Hide the Legend
            histogramChart.HasLegend = false;

            // Show data labels
            var labels = histogramChart.Series[0].DataPoints.DefaultDataPoint.DataLabels;
            labels.HasValue = true;
            labels.Size = 9;

            // Save the file
            workbook.SaveToFile("Histogram_chart.xlsx", ExcelVersion.Version2016);
            workbook.Dispose();

        }
    }
}

After running the code, open the Histogram_chart.xlsx file, you will see a fully rendered histogram with the bin settings, titles, and formatting applied.

Create an Excel histogram chart with C# and Spire.XLS

Frequently Asked Questions

Q: What is the difference between BinWidth and NumberOfBins?

  • BinWidth fixes the width of each interval and calculates the number of bins automatically.
  • NumberOfBins fixes the number of intervals and calculates the width automatically.

For histograms, you generally use one or the other depending on whether you want consistent interval sizes or a specific level of detail.

Q: Does Spire.XLS support exporting the histogram to PDF or images?

Yes. After building the chart, you can call workbook.SaveToFile("output.pdf", FileFormat.PDF) or use the chart‑to‑image conversion methods to export the chart as an image for reports or web dashboards.

Q: Can I create multiple histograms in one workbook?

Yes. Add separate charts to worksheets or to different positions on the same sheet. For multiple distributions, creating one histogram per numeric column is usually clearer than combining them.

Q: Is a histogram the same as a column chart?

No. A histogram shows the distribution of continuous numerical data across bins; a column chart compares values across separate categories.

Conclusion

Making a histogram chart in Excel with C# is a straightforward process when you use Spire.XLS for .NET. By following the steps in this guide, developers can embed automated histogram generation directly into .NET workflows, eliminating manual chart creation in Excel. It supports flexible customization of axes, styles, and chart titles to fit different reporting and data analysis requirements.

Explore the official documentation for additional charting and Excel‑processing capabilities.